import common from '../vendor/common'
import '../scss/user_revise_password.scss'
import { resetRwd } from '../vendor/api'
try{
    const mui = common.mui
    var old_pwd = null //旧密码输入值
    var new_pwd = null //新密码输入值
    var confirmPwd = null //再次确认密码输入值
    var data = {}
    mui.init({
        swipeBack:true //启用右滑关闭功能
    });
    mui.plusReady(function(){
        // 清除输入框信息
        window.addEventListener('revise', function(e){
            old_pwd = null
            new_pwd = null
            confirmPwd = null
            data = {}
            mui('#oldPwd')[0].value = null
            mui('#newPwd')[0].value = null
            mui('#confirmPwd')[0].value = null
        })
        // 修改密码
        mui('.loginOut').on('tap','button',function(){
            // 获取输入框的值
            old_pwd = mui('#oldPwd')[0].value
            new_pwd = mui('#newPwd')[0].value
            confirmPwd = mui('#confirmPwd')[0].value
            data.old_pwd = old_pwd
            data.new_pwd = new_pwd
            // 隐藏键盘
            mui('#oldPwd')[0].blur()
            mui('#newPwd')[0].blur()
            mui('#confirmPwd')[0].blur()
            // 验证并修改
            if(!old_pwd || old_pwd.trim() == ''){
                mui.alert('请输入旧密码')
            } else if(!new_pwd || new_pwd.trim() == ''){
                mui.alert('请输入新密码')
            } else if(!confirmPwd || confirmPwd.trim() == ''){
                mui.alert('确认密码不能为空')
            } else if(old_pwd.length < 6 || old_pwd.length > 18){
                mui.alert('密码长度必须在6-18位之间')
            } else if(new_pwd.length < 6 || new_pwd.length > 18){
                mui.alert('密码长度必须在6-18位之间')
            } else if(new_pwd !== confirmPwd){
                mui.alert('确认密码与新密码必须一致')
            } else {
                resetRwd(data).then((res) => {
                    console.log(JSON.stringify(res))
                    if(res.data.status){
                        mui.toast('密码修改成功')
                        // 获取设置页面webview
                        var setPage = plus.webview.getWebviewById('user_setting.html')
                        // 获取当前页面webview
                        var ws=plus.webview.currentWebview();
                        plus.webview.hide(ws)
                        plus.webview.hide(setPage)
                    } else {
                        mui.toast(res.data.message)
                    }
                })
            }
        })
    })
}catch(e){
    console.log('user_revise_password.html出错了')
}